package com.hzcx.app.simplechat.util.map;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.poisearch.PoiSearch;
import com.hzcx.app.simplechat.MyApplication;

/**
 *  地图定位相关
 */
public class GdMapUtils {

    public static GdMapUtils gdMapUtils=null;

    public static GdMapUtils getInstance(){
        if(gdMapUtils==null){
            gdMapUtils=new GdMapUtils();
        }
        return gdMapUtils;
    }

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
    private PoiSearch.Query query;

    /**
     *  开始定位
     * @param listener
     */
    public void startLocation(AMapLocationListener listener) {
        //初始化定位
        try {
            mLocationClient = new AMapLocationClient(MyApplication.getContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //设置定位回调监听
        mLocationClient.setLocationListener(listener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption.setOnceLocation(true);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否允许模拟位置,默认为true，允许模拟位置
        mLocationOption.setMockEnable(true);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(true);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    /**
     *  停止定位
     */
    public void stopLocation(){
        if(mLocationClient!=null){
            mLocationClient.stopLocation();
        }
    }

    /**
     * 查询附近地址
     *
     * @param aMapLocation
     */
    public void getAddressList(AMapLocation aMapLocation, PoiSearch.OnPoiSearchListener listener) {
        query = new PoiSearch.Query("", "", aMapLocation.getCity());
        query.setPageSize(20);// 设置每页最多返回多少条poiitem
        query.setPageNum(1);// 设置查第一页
        LatLonPoint lpTemp = new LatLonPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude());
        if (lpTemp != null) {
            PoiSearch poiSearch = null;
            try {
                poiSearch = new PoiSearch(MyApplication.getContext(), query);
            } catch (AMapException e) {
                e.printStackTrace();
            }
            poiSearch.setBound(new PoiSearch.SearchBound(lpTemp, 5000, true));//
            poiSearch.searchPOIAsyn();// 异步搜索
            poiSearch.setOnPoiSearchListener(listener);
        }
    }

    /**
     * 查询附近地址
     *
     */
    public void getAddressList(double lat,double lng, PoiSearch.OnPoiSearchListener listener) {
        query = new PoiSearch.Query("", "", "");
        query.setPageSize(20);// 设置每页最多返回多少条poiitem
        query.setPageNum(1);// 设置查第一页
        LatLonPoint lpTemp = new LatLonPoint(lat, lng);
        if (lpTemp != null) {
            PoiSearch poiSearch = null;
            try {
                poiSearch = new PoiSearch(MyApplication.getContext(), query);
            } catch (AMapException e) {
                e.printStackTrace();
            }
            poiSearch.setBound(new PoiSearch.SearchBound(lpTemp, 5000, true));//
            poiSearch.searchPOIAsyn();// 异步搜索
            poiSearch.setOnPoiSearchListener(listener);
        }
    }

    /**
     *  根据关键字搜索地址
     * @param key
     */
    public void getAddressList(String key, String city, PoiSearch.OnPoiSearchListener listener) {
        query = new PoiSearch.Query(key, "", city);
        query.setPageSize(20);// 设置每页最多返回多少条poiitem
        query.setPageNum(1);// 设置查第一页
        PoiSearch poiSearch = null;
        try {
            poiSearch = new PoiSearch(MyApplication.getContext(), query);
        } catch (AMapException e) {
            e.printStackTrace();
        }
        poiSearch.searchPOIAsyn();// 异步搜索
        poiSearch.setOnPoiSearchListener(listener);
    }

    /**
     *  获取高德的query
     * @return
     */
    public PoiSearch.Query getQuery(){
        return query;
    }
}
